import { IDomEditor, IButtonMenu } from '@wangeditor/editor';
import * as dayjs from 'dayjs';
//插入日期的按钮
class InsertDate implements IButtonMenu {
  // TS 语法
  title: string;
  iconSvg?: string;
  tag: string;
  constructor() {
    this.title = '插入日期'; // 自定义菜单标题
    this.iconSvg =
      '<svg t="1704330747531" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6959" width="200" height="200"><path d="M890.127 145.672h-43.389V132.78c0-38.644-31.466-70.234-69.922-70.234h-1.032c-38.458 0-69.922 31.59-69.922 70.234v12.892h-386.7V132.78c0-38.644-31.465-70.234-69.922-70.234h-1.063c-38.457 0-69.923 31.59-69.923 70.234v12.892h-43.296c-38.458 0-69.923 31.622-69.923 70.207v671.446c0 38.644 31.466 70.236 69.923 70.236h755.17c38.458 0 69.922-31.592 69.922-70.236V215.879c0-38.585-31.464-70.207-69.923-70.207zM733.83 132.78c0-23.222 18.793-42.139 41.954-42.139h1.032c23.13 0 41.954 18.918 41.954 42.139v124.927c0 23.224-18.824 42.14-41.954 42.14h-1.032c-23.161 0-41.954-18.916-41.954-42.14V132.779z m-527.607 0c0-23.222 18.793-42.139 41.955-42.139h1.063c23.098 0 41.954 18.918 41.954 42.139v124.927c0 23.224-18.856 42.14-41.954 42.14h-1.063c-23.162 0-41.955-18.916-41.955-42.14V132.779z m683.904 768.626H134.958c-7.585 0-13.985-6.525-13.985-14.08V393.649h783.138v493.676c0 7.555-6.399 14.08-13.984 14.08z" fill="" p-id="6960"></path><path d="M223.755 494.2H352.78v63.488H223.755V494.2zM449.035 494.2h128v63.488h-128V494.2zM673.291 494.2h125.952v63.488H673.291V494.2zM223.755 623.224H352.78v62.464H223.755v-62.464zM449.035 623.224h128v62.464h-128v-62.464zM673.291 623.224h125.952v62.464H673.291v-62.464zM223.755 749.176H352.78v65.536H223.755v-65.536zM449.035 749.176h128v65.536h-128v-65.536zM673.291 749.176h125.952v65.536H673.291v-65.536z" fill="" p-id="6961"></path></svg>'; // 可选
    this.tag = 'button';
  }
  // 获取菜单执行时的 value ，用不到则返回空 字符串或 false
  getValue(editor: IDomEditor): string | boolean {
    // TS 语法
    return ' hello ';
  }
  // 菜单是否需要激活（如选中加粗文本，“加粗”菜单会激活），用不到则返回 false
  isActive(editor: IDomEditor): boolean {
    // TS 语法
    return false;
  }
  // 菜单是否需要禁用（如选中 H1 ，“引用”菜单被禁用），用不到则返回 false
  isDisabled(editor: IDomEditor): boolean {
    // TS 语法
    return false;
  }
  // 点击菜单时触发的函数
  exec(editor: IDomEditor, value: string | boolean) {
    // TS 语法
    if (this.isDisabled(editor)) {
      return;
    }
    // editor.insertText(value) // value 即 this.value(editor) 的返回值
    const date = new Date();
    const insertText = dayjs(date).format('YYYY年MM月DD日 dddd');
    // editor.dangerouslyInsertHtml(`<p>${replaceInsertDate(insertText)}</p><br/>`)
    const node = {
      type: 'time-line',
      date: replaceInsertDate(insertText),
      children: [
        {
          text: '',
        },
      ],
    };
    editor.insertNode(node);
    // const node: any = {
    //   type: "link-card",
    //   title: "百度一下，你就知道",
    //   link: "https://www.baidu.com/",
    //   children: [
    //     {
    //       text: ""
    //     }
    //   ]
    // }
    // editor.insertNode(node)
  }
}

function replaceInsertDate(insertText: string) {
  const weekMap = new Map([
    ['Monday', '星期一'],
    ['Tuesday', '星期二'],
    ['Wednesday', '星期三'],
    ['Thursday', '星期四'],
    ['Friday', '星期五'],
    ['Saturday', '星期六'],
    ['Sunday', '星期日'],
  ]);
  weekMap.forEach((value, key) => {
    if (insertText.includes(key)) {
      insertText = insertText.replace(key, value);
    }
  });
  return insertText;
}
export default InsertDate;
